package com.example.lopngoaingu;

import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

public class PlayAudioActivity extends Activity {
	private Button btnPlay, btnStop, btnDuration;
	private TextView tvDuration, tvPlayTime;
	private SeekBar seekBar;
	private MediaPlayer player;
	private Handler playerHandler;
	private Runnable runmp3;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.playaudio_layout);
		btnPlay = (Button) findViewById(R.id.btnPlay);
		btnStop = (Button) findViewById(R.id.btnStop);
		btnDuration = (Button) findViewById(R.id.btnDuration);
		tvDuration = (TextView) findViewById(R.id.tvDuration);
		tvPlayTime = (TextView) findViewById(R.id.tvPlayTime);
		seekBar = (SeekBar) findViewById(R.id.seekBar);
		playerHandler = new Handler();
		btnPlay.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				try {
					// MediaPlayer player =
					// MediaPlayer.create(PlayAudioActivity.this, R.raw.a1) ;
					player = new MediaPlayer();
					player.setDataSource("/data/data/com.example.lopngoaingu/ESS4/11.mp3");
					player.prepare();
					player.start();
					tvDuration.setText(String.format("%02d",
							player.getDuration() / 1000 / 60)
							+ ":"
							+ String.format("%02d",
									player.getDuration() / 1000 % 60));
					playerHandler.postDelayed(runmp3, 100);
					// new PlayAudio().execute();

				} catch (Exception e) {
					Log.i("Error Code", e.getMessage());
				}
			}
		});

		runmp3 = new Runnable() {
			@Override
			public void run() {
				int mTimeLeft = player.getCurrentPosition();
				int mDuration = player.getDuration();
				tvPlayTime.setText(String.format("%02d", mTimeLeft / 1000 / 60)
						+ ":" + String.format("%02d", mTimeLeft / 1000 % 60));
				int percent = (mTimeLeft * 100) / mDuration;
				seekBar.setProgress(percent);
				playerHandler.postDelayed(runmp3, 100);
			}
		};

		/*
		 * player.setOnCompletionListener(new OnCompletionListener() {
		 * 
		 * @Override public void onCompletion(MediaPlayer mp) {
		 * 
		 * } });
		 */

		seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
				int time = (int) (((double) seekBar.getProgress() / 100))
						* player.getDuration();
				player.seekTo(time);
				playerHandler.postDelayed(runmp3, 100);
			}

			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
				playerHandler.removeCallbacks(runmp3);
			}

			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				// TODO Auto-generated method stub

			}
		});

		btnStop.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				player.stop();
			}
		});

		btnDuration.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
			}
		});
	}

	class PlayAudio extends AsyncTask<Void, Integer, Void> {

		@Override
		protected void onPreExecute() {
			try {
				player = new MediaPlayer();
				player.setDataSource("/data/data/com.example.lopngoaingu/ESS4/1.mp3");
				player.prepare();
				player.start();
			} catch (Exception e) {
			}
			super.onPreExecute();
		}

		@Override
		protected Void doInBackground(Void... params) {
			while (player.isPlaying()) {
				try {
					publishProgress(player.getCurrentPosition());
					Thread.sleep(1000);
				} catch (Exception e) {
				}
			}
			return null;
		}

		@Override
		protected void onProgressUpdate(Integer... values) {
			tvPlayTime.setText(String.format("%02d", values[0] / 1000 / 60)
					+ ":" + String.format("%02d", values[0] / 1000 % 60));
			int percent = (values[0] * 100) / player.getDuration();
			seekBar.setProgress(percent);
			super.onProgressUpdate(values);
		}
	}

}
